//
//  VerifyViewController.m
//  TouchID
//
//  Created by razeen on 12/23/15.
//  Copyright © 2015 razeen. All rights reserved.
//

#import "VerifyViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import "AppDelegate.h"
#import "SettingTableViewController.h"

@interface VerifyViewController ()

@property (weak, nonatomic) IBOutlet UIButton *touchBtn;

- (IBAction)touchClick:(id)sender;


@end

@implementation VerifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self figuteTouch];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)figuteTouch{
    
    //初始化对象
    LAContext *context = [[LAContext alloc]init];
    //错误对象
    NSError *error = nil;
    
    NSString *result = @"通过Home键验证已有手机指纹";
    
    context.localizedFallbackTitle = @"验证手势密码";
    //先使用can Evaluate Policy判断设备支持状态
    
    BOOL canEvaluate = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    
    if (canEvaluate) {
        //支持指纹验证
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError * _Nullable error) {
            
            if (success) {
                
                NSLog(@"success");
                //切换主线程处理
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    
                    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
                    
                    UIStoryboard *SB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    
                    SettingTableViewController *STVC = [SB instantiateViewControllerWithIdentifier:@"setting"];
                    
                    appDelegate.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:STVC];
                }];

            }else{
                
                NSLog(@"%@",error.localizedDescription);
                
                switch (error.code) {
                    case LAErrorSystemCancel:
                        
                        NSLog(@"Authentication was cancelled by the system");
                        //切换到其他APP，系统取消验证Touch ID
                        break;
                    case LAErrorUserCancel:
                        
                        NSLog(@"Authentication was cancelled by the user");
                        //用户取消验证Touch ID
                        break;
                    case LAErrorUserFallback:
                        
                        NSLog(@"User selected to enter custom password");
                        
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //用户选择其他验证方式，切换主线程处理
                        }];
                        break;
                        
                    default:
                        
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //其他情况，切换主线程处理
                        }];
                        
                        break;
                }
            }
        }];
    }else{
        //不支持指纹识别，LOG出错误详情
        
        switch (error.code) {
            case LAErrorTouchIDNotEnrolled:
            {
                NSLog(@"TouchID is not enrolled");
                break;
            }
            case LAErrorPasscodeNotSet:
            {
                NSLog(@"A passcode has not been set");
                break;
            }
            default:
            {
                NSLog(@"TouchID not available");
                break;
            }
        }
        
        NSLog(@"%@",error.localizedDescription);
        
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)touchClick:(id)sender {
    
    
    [self figuteTouch];

}
@end
